﻿package Cain.bom 
{
	import Cain.all_mk;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import Cain.mc_cesh.Cesher;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import gs.easing.*;
	import gs.TweenMax;
	/**
	 * 暴走視窗 v1.0
	 * @author Cain
	 */
	public class Bom_1 extends Sprite
	{
		private var ss:int;
		private var _root:Sprite; //場景
		private var bg:Bitmap;
		private var arr:Array;
		private var arr_sp:Sprite;
		private var mk:Sprite;
		/**
		 * 螢幕爆炸
		 * @param	_mc 場景
		 * @param	_s 切割寬度
		 */
		public function Bom_1(_mc:Sprite,_s:int = 50) 
		{
			ss = _s;
			_root = Sprite(_mc.root);
			setup();
		}
		
		private function setup():void
		{
			addEventListener(Event.ADDED_TO_STAGE, addThid);
			_root.addChild(this);
		}
		
		private function addThid(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addThid);
			addEventListener(Event.REMOVED_FROM_STAGE,removeThis);
			init();
		}
		
		private function init():void
		{
			bg = new Bitmap(cut_back());//咖圖
			arr_setup();//切圖
			//down_it();//落下
		}
		/**
		 * 落下
		 */
		public function down_it():void //落下
		{
			
			mk = new Sprite();
			mk.graphics.beginFill(0);
			mk.graphics.drawRect(0, 0, _root.width, _root.height);
			mk.graphics.endFill();
			addChild(mk);
			addChild(arr_sp);
			for (var i:int = 0; i < arr.length; i++) 
			{
				arr_sp.addChild(arr[arr.length - i - 1]);
				TweenMax.to(arr[i], 1, { y:"1000", alpha:0,rotation:Math.random()*360-180, delay:Math.random()*2 ,ease:Sine.easeIn} );
			}
			var game:Loader = new Loader();
			game.load(new URLRequest("game.swf"));
			mk.addChild(game);
		}
		private function arr_setup():void //切圖
		{
			var w_n:int = bg.width / ss;
			var h_n:int = bg.height / ss;
			var last_w:int = bg.width % ss;
			var last_h:int = bg.height % ss;
			if (last_w>0) 
			{
				w_n++;
			}else 
			{
				last_w = ss;
			}
			if (last_h>0) 
			{
				h_n++;
			}else 
			{
				last_h = ss;
			}
			arr = new Array();
			arr_sp = new Sprite();
			for (var i:int = 0; i < h_n; i++) 
			{
				for (var j:int = 0; j < w_n; j++) 
				{
					var _ws:int = ss;
					var _hs:int = ss;
					if (j==w_n-1) 
					{
						_ws = last_w;
					}
					if (i==h_n-1) 
					{
						_hs = last_h;
					}
					var _ca:Cesher = new Cesher(bg, new Rectangle(j * ss, i * ss, _ws, _hs));
					var _bt:Bitmap = new Bitmap(_ca.bitmapData());
					var btmc:Sprite = new Sprite();
					btmc.addChild(_bt)
					btmc.x = j * ss;
					btmc.y = i * ss;
					arr_sp.addChild(btmc);
					arr.push(btmc);
				}
			}
			addChild(arr_sp);
		}
		
		private function cut_back():BitmapData //咖圖
		{
			var ca:Cesher = new Cesher(_root, new Rectangle(0, 0, _root.width, _root.height));
			return(ca. bitmapData());
			
		}
		/**
		 * 移除
		 */
		public function unload():void 
		{
			parent.removeChild(this);
		}
		private function removeThis():void
		{
			
		}
		
	}

}